package com.example.retroiftdemo_229;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginActivity extends AppCompatActivity {
    private  String userid;
    private EditText name,mima;
    private ImageView p1,p2;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
    }

    private void initViews() {
        name=findViewById(R.id.editText);
        mima=findViewById(R.id.editText2);
        p1=findViewById(R.id.imageView3);
        p2=findViewById(R.id.imageView2);
        button=findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Retrofit retrofit=new Retrofit.Builder()
                        .baseUrl("http://172.24.10.130:8080/foodService/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                UserService userService=retrofit.create(UserService.class);
                Call<LoginBean> call=userService.login(name.getText().toString(),mima.getText().toString());


                call.enqueue(new Callback<LoginBean>() {
                    @Override
                    public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                        userid=response.body().getUserid();
                        if("0".equals(userid)) {
                            Toast.makeText(LoginActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
                            System.out.println("--登陆失败");
                        }
                        else {
                            Toast.makeText(LoginActivity.this, "登     陆     成    功", Toast.LENGTH_SHORT).show();
                            System.out.println("--登陆成功");
                        }
                    }

                    @Override
                    public void onFailure(Call<LoginBean> call, Throwable t) {
                        Toast.makeText(LoginActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                      System.out.println("--网络错误");
                    }
                });


            }
        });

    }
}
